/**
  ******************************************************************************
  * @file    main.c
  * @author  AE Team
  * @version 1.0.3
  * @date    2024-05-28
  * @brief   演示GTIM1边沿对齐模式，向上计数，PCLK作为GTIM1时钟源
  *          配置PCLK = HCLK = SYSCLK = HSI48MHz, 预分频48000 - 1, 重载值为100 - 1
  *          (溢出频率为 48000000 / 48000 / 100 = 10, 溢出时间为 1/10 = 100mS)
  *          开启GTIM1溢出中断，则每1mS触发一次中断。
  *          中断服务程序中，检测到溢出标志翻转LED(PA03)
  *
  * @note    硬件平台：SYM32L010_StartKit_V1.0
  *          接线说明：短接J28
  *
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2024 SIYIMicro.
  * All rights reserved.</center></h2>
  *
  *
  ******************************************************************************
  */


/******************************************************************************/
/* Include files                                                              */
/******************************************************************************/
#include "main.h"
#include "sym32l010_hal_sysctrl.h"
#include "sym32l010_hal_systick.h"
#include "sym32l010_hal_gpio.h"
#include "sym32l010_hal_gtim.h"
#include "sym32l010_Startkit_board.h"


/******************************************************************************/
/* Local pre-processor symbols/macros ('#define')                             */
/******************************************************************************/

/******************************************************************************/
/* Global variable definitions (declared in header file with 'extern')        */
/******************************************************************************/
GTIM_HandleTypeDef hgtim1;

/******************************************************************************/
/* Local type definitions ('typedef')                                         */
/******************************************************************************/

/******************************************************************************/
/* Local function prototypes ('static')                                       */
/******************************************************************************/
static void GTIM1_Init(void);

/******************************************************************************/
/* Local variable definitions ('static')                                      */
/******************************************************************************/

/******************************************************************************/
/* Local pre-processor symbols/macros ('#define')                             */
/******************************************************************************/

/******************************************************************************/
/* Function implementation - global ('extern') and local ('static')           */
/******************************************************************************/


/*******************************************************************************
 ** \brief  Main function of project
 **
 ** \return uint32_t return value, if needed
 **
 **
 ******************************************************************************/
int32_t main(void)
{
    // 初始化系统时钟
    HAL_SysCtrl_ConfigWaitCycle(48000000);
    HAL_SysCtrl_HSI_SetDiv(HSIOSC_TO_HSI48MHZ);

    // 初始化SysTick
    HAL_InitTick(48000000);

    // 初始化板载LED
    Bsp_Led_Init();

    // 初始化GTIM1
    GTIM1_Init();

    while (1)
    {
        ;  /* 中断服务程序见回调函数GTIM1_IRQHandlerCallBack */
    }
}


/*******************************************************************************
 * @brief  GTIM1初始化
 * @param  None
 * @retval None
 */
static void GTIM1_Init(void)
{
    HAL_SYSCTRL_GTIM1_CLK_ENABLE();

    // 配置ATIM
    MemClr(&hgtim1, sizeof(hgtim1));

    hgtim1.Instance               = SYM_GTIM1;
    hgtim1.Init.ClockDivision     = GTIM_CLOCKDIVISION_DIV1;
    hgtim1.Init.CounterMode       = GTIM_COUNTERMODE_UP;
    hgtim1.Init.Prescaler         = 48000 - 1;
    hgtim1.Init.Period            = 100 - 1;                          // 溢出周期100ms
    hgtim1.Init.AutoReloadPreload = GTIM_AUTORELOAD_PRELOAD_DISABLE;
    HAL_GTIM_Base_Init(&hgtim1);

    NVIC_ClearPendingIRQ(GTIM1_IRQn);
    NVIC_EnableIRQ(GTIM1_IRQn);

    HAL_GTIM_Base_Start_IT(&hgtim1);                                  // 启动GTIM1并开启更新中断, 在中断里改变比较值
}


/*******************************************************************************
 * @brief  GTIM1中断服务程序回调函数, 进行LED翻转
 * @param  None
 *
 * @retval None
 */
void GTIM1_IRQHandlerCallBack(void)
{
    if (HAL_GTIM_GET_IT_SOURCE(&hgtim1, GTIM_IT_SOURCE_UPDATE) &&
        HAL_GTIM_GET_FLAG(&hgtim1, GTIM_IT_FLAG_UPDATE))
    {
        HAL_GTIM_CLR_FLAG(&hgtim1, GTIM_IT_FLAG_UPDATE);

        BSP_LED_TOG();
    }
}


/*******************************************************************************
 ** EOF (not truncated)
 ******************************************************************************/
#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
    /* USER CODE BEGIN */
    /* User can add his own implementation to report the file name and line number,
       tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
    /* USER CODE END */
}
#endif /* USE_FULL_ASSERT */


/************************ (C) COPYRIGHT SIYIMicro *****END OF FILE*************/
